home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / STRINSML.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  3KB  |  150 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7.         extrn    sl_malloc:far
  8. ;
  9. ;
  10. ; strinsml-     Inserts one string within another.
  11. ;
  12. ; inputs:
  13. ;
  14. ;    ES:DI-     Points at destination string, the one into which the source
  15. ;               string will be appended.
  16. ;
  17. ;    CS:RET-    Points at the string to insert.
  18. ;
  19. ;    CX-    Index into source string (ES:DI) to begin insertion.
  20. ;
  21. ; outputs:
  22. ;
  23. ;    ES:DI-    Points at new string on the heap.
  24. ;    Carry-    Zero if no error, One if memory allocation error.
  25. ;
  26. ;
  27. ;
  28.         public    sl_strinsml
  29. ;
  30. rtnadrs        equ    2[bp]
  31. srcseg        equ    (0-2)[bp]
  32. source        equ    (0-4)[bp]
  33. destseg        equ    (0-6)[bp]
  34. dest        equ    (0-8)[bp]
  35. insindx        equ    (0-10)[bp]
  36. ;
  37. sl_strinsml    proc    far
  38.         push    bp
  39.         mov    bp, sp
  40.         push    dx        ;Dummy spot
  41.         push    si        ;Dummy spot
  42.         push    es
  43.         push    di
  44.         push    cx
  45.         pushf
  46.         push    ax
  47.         push    bx
  48.         push    ds
  49.         push    dx
  50.         push    si
  51. ;
  52.         mov    si, rtnadrs
  53.         mov    source, si
  54.         mov    dx, rtnadrs+2
  55.         mov    srcseg, dx
  56.         cld
  57. ;
  58. ; Compute the length of the destination string.
  59. ;
  60.         mov    al, 0
  61.         mov    cx, 0ffffh
  62.     repne    scasb
  63. ;
  64. ; Compute the length of the string to insert.
  65. ;
  66.         mov    di, si
  67.         mov    es, dx            ;(srcseg)
  68.         mov    al, 0
  69.     repne    scasb
  70.         mov    rtnadrs, di        ;Restore return address.
  71.         neg    cx
  72.         dec    cx
  73. ;;;;;;;;;;;;;;    dec    cx            ;Plus one for the zero byte.
  74. ;
  75. ; Allocate memory for the new string:
  76. ;
  77.         call    sl_malloc
  78.         jnc    GoodMalloc
  79. ;
  80.         pop    si
  81.         pop    dx
  82.         pop    ds
  83.         pop    bx
  84.         pop    ax
  85.         popf
  86.         pop    cx
  87.         pop    di
  88.         pop    es
  89.         add    sp, 4            ;Remove junk bytes
  90.         pop    bp
  91.         stc                ;Out of memory
  92.         ret
  93. ;
  94. ;
  95. ; If we were able to malloc the string, drop down here:
  96. ;
  97. GoodMalloc:    mov    dx, es            ;Save ptr to new string
  98.         mov    bx, di
  99.         lds    si, dword ptr Dest
  100. ;
  101. ; Copy the first part of the destination string to the new string.
  102. ;
  103.         mov    cx, insindx
  104. CpyDest1:    lodsb
  105.         stosb
  106.         cmp    al, 0
  107.         loopne    CpyDest1
  108.         jnz    SkipDec
  109.         dec    di            ;Back up a character if we
  110.         dec    si            ; hit a zero byte.
  111. ;
  112. SkipDec:    push si                ;Save ptr to middle of dest.
  113. ;
  114. ; Copy the source string into the middle.
  115. ;
  116.         lds    si,source
  117. CpySrc:        lodsb
  118.         stosb
  119.         cmp    al, 0
  120.         jnz    CpySrc
  121.         dec    di
  122. ;
  123. ; Copy the remainder of the destination string to the new string.
  124. ;
  125.         pop    si            ;Retrieve ptr into dest.
  126.         mov    ds, DestSeg
  127. CpyDest:    lodsb
  128.         stosb
  129.         cmp    al, 0
  130.         jnz    CpyDest
  131.         mov    es, dx            ;Retrieve ptr to new string.
  132.         mov    di, bx
  133. ;
  134.         pop    si
  135.         pop    dx
  136.         pop    ds
  137.         pop    bx
  138.         pop    ax
  139.         popf
  140.         pop    cx
  141.         add    sp,8            ;Don't restore es:di or junk.
  142.         pop    bp
  143.         clc                ;Allocated string okay.
  144.         ret
  145. sl_strinsml    endp
  146. ;
  147. ;
  148. stdlib        ends
  149.         end
  150.